package cn.usts.edu.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ChengActionEvent {
    public static void main(String[] args) {
        new ChengFrame();
    }
}


class ChengFrame extends Frame{

    // 6*文本框   2*按钮  2*标签   [ce=cheng, cu=chu]
    TextField  ce1,ce2,ce3,cu1,cu2,cu3;
    Button buttonCe,buttonCu;
    Label labelCe,labelCu;

    public ChengFrame()  {
        setVisible(true);
        // 位置大小
        setBounds(400,400,100,100);
        ce1 = new TextField(10);
        ce2 = new TextField(10);
        ce3 = new TextField(10);
        cu1 = new TextField(10);
        cu2 = new TextField(10);
        cu3 = new TextField(10);

        buttonCe = new Button("=");
        buttonCu = new Button("=");

        // 按钮动作监听
        buttonCu.addActionListener(new ActionListenerCu(cu1,cu2,cu3));
        buttonCe.addActionListener(new ActionListenerCe(ce1,ce2,ce3));
        // 窗口退出监听
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


        labelCe = new Label("   *");
        labelCu = new Label("   ÷");

        setLayout(new GridLayout(2,1));
        add(ce1);
        add(labelCe);
        add(ce2);
        add(buttonCe);
        add(ce3);

        add(cu1);
        add(labelCu);
        add(cu2);
        add(buttonCu);
        add(cu3);


    }
}

// 乘法监听器
class ActionListenerCe implements ActionListener{
    private TextField ce1,ce2,ce3;

    public ActionListenerCe(TextField ce1, TextField ce2, TextField ce3) {
        this.ce1 = ce1;
        this.ce2 = ce2;
        this.ce3 = ce3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int ce01 = Integer.parseInt(ce1.getText());
        int ce02 = Integer.parseInt(ce2.getText());
        ce3.setText(""+(ce01*ce02));
        ce1.setText("");
        ce2.setText("");
    }
}

// 除法监听器
class ActionListenerCu implements ActionListener {
    private TextField cu1,cu2,cu3;

    public ActionListenerCu(TextField cu1, TextField cu2, TextField cu3) {
        this.cu1 = cu1;
        this.cu2 = cu2;
        this.cu3 = cu3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int cu01 = Integer.parseInt(cu1.getText());
        int cu02 = Integer.parseInt(cu2.getText());
        cu3.setText(""+(cu01/cu02));
        cu1.setText("");
        cu2.setText("");
    }
}